package springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.domain.LoginResult;
import springboot.service.LoginService;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping(value = "/login")
    public void login(@RequestParam("username") String username, @RequestParam("password") String password) {
        System.out.println(username + " login()");
        LoginResult loginResult = loginService.login(username,password);
        if(loginResult.isLogin()){
            System.out.println("登陆成功！");
        } else {
            throw new RuntimeException("登录失败：" + loginResult.getResult());
        }
    }
    @GetMapping(value = "/logout")
    @ResponseBody
    public String logout() {
        String name = loginService.logout();
        return "用户" + name + "退出登录！";
    }
}

